﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using Oracle.DataAccess.Client;
using Sweepids.Models.Dao;

namespace Sweepids.Models.Object
{
    public class Item
    {
        //Private members
        private bool _fill = false;
        private Category _categoryObj;
        private User _winUserObj;
        private List<User> _bidOnUsersList;
        private List<User> _loseUsersList;
        private List<BidOn> _bidOnsList; 

        //Columns
        public decimal Id { get; set; }
        public string Name { get; set; }
        public decimal Price { get; set; }
        public DateTime StartTime { get; set; }
        public DateTime EndTime { get; set; }
        public decimal SweepsOnItem { get; set; }
        public string Description { get; set; }
        public decimal OriginalPrice { get; set; }
        public string WinUser { get; set; }
        public string Photo { get; set; }
        public decimal CatId { get; set; }
        //Foreign Keys
        public Category CategoryObj
        {
            get
            {
                if (!_fill)
                {
                    var categoryDao = new CategoryDao();
                    _categoryObj = categoryDao.Read(CatId);
                }
                return _categoryObj;
            }
            set
            {
                _categoryObj = value;
                CatId = value.CatId;
            }
        }

        public User WinUserObj
        {
            get
            {
                if (!_fill)
                {
                    var userDao = new UserDao();
                    _winUserObj = userDao.Read(WinUser);
                }
                return _winUserObj;
            }
            set
            {
                _winUserObj = value;
                WinUser = value.Username;
            }
        }
        //Extra details
        public List<BidOn> BidOnsList
        {
            get
            {
                if (!_fill)
                {
                    var bidOnDao = new BidOnDao();
                    var bidOnList = bidOnDao.Read(Id);
                    var list = bidOnList.ToList();
                    _bidOnsList = list;
                }
                return _bidOnsList;
            }
        } 
        public List<User> BidOnUsersList
        {
            get
            {
                if (!_fill)
                {
                    var bidOnDao = new BidOnDao();
                    var bidOnList = bidOnDao.Read(Id);
                    _bidOnUsersList = bidOnList.Select(bidOn => bidOn.UserObj).ToList();
                }
                return _bidOnUsersList;
            }
        }
        public List<User> LoseUsersList
        {
            get
            {
                if (!_fill)
                {
                    var loseDao = new LoseDao();
                    var loseList = loseDao.Read(Id);
                    _loseUsersList = loseList.Select(lose => lose.UserObj).ToList();
                }
                return _loseUsersList;
            }
        }
        

        //Oracle parameters
        public List<OracleParameter> OracleParameters
        {
            get
            {
                return new List<OracleParameter>
                    {
                        new OracleParameter(":ID", OracleDbType.Decimal)
                            {
                                Value = Id
                            },
                        new OracleParameter(":NAME", OracleDbType.Varchar2)
                            {
                                Value = Name ?? ""
                            },
                        new OracleParameter(":PRICE", OracleDbType.Decimal)
                            {
                                Value = Price
                            },
                        new OracleParameter(":START_TIME", OracleDbType.Date)
                            {
                                Value = StartTime
                            },
                        new OracleParameter(":END_TIME", OracleDbType.Date)
                            {
                                Value = EndTime
                            },
                        new OracleParameter(":SWEEPS_ON_ITEM", OracleDbType.Decimal)
                            {
                                Value = SweepsOnItem
                            },
                        new OracleParameter(":DESCRIPTION", OracleDbType.Varchar2)
                            {
                                Value = Description ?? ""
                            },
                        new OracleParameter(":ORIGINAL_PRICE", OracleDbType.Decimal)
                            {
                                Value = OriginalPrice
                            },
                        new OracleParameter(":WIN_USER", OracleDbType.Varchar2)
                            {
                                Value = WinUser ?? ""
                            },
                        new OracleParameter(":PHOTO", OracleDbType.Varchar2)
                            {
                                Value = Photo ?? ""
                            },
                        new OracleParameter(":CAT_ID", OracleDbType.Decimal)
                            {
                                Value = CatId
                            }
                    };
            }
        }

        //Constructors
        public Item()
        {
            Name = "";
        }

        public Item(IDataRecord myReader)
        {
            Id = myReader["ITEM_ID"] == DBNull.Value ? 0 : (decimal) myReader["ITEM_ID"];
            Name = myReader["NAME"] == DBNull.Value ? "" : (string) myReader["NAME"];
            Price = myReader["PRICE"] == DBNull.Value ? 0 : (decimal)myReader["PRICE"];
            StartTime = myReader["START_TIME"] == DBNull.Value ? DateTime.Now : (DateTime) myReader["START_TIME"];
            EndTime = myReader["END_TIME"] == DBNull.Value ? DateTime.Now : (DateTime) myReader["END_TIME"];
            SweepsOnItem = myReader["SWEEPS_ON_ITEM"] == DBNull.Value ? 0 : (decimal) myReader["SWEEPS_ON_ITEM"];
            Description = myReader["DESCRIPTION"] == DBNull.Value ? "" : (string) myReader["DESCRIPTION"];
            OriginalPrice = myReader["ORIGINAL_PRICE"] == DBNull.Value ? 0 : (decimal)myReader["ORIGINAL_PRICE"];
            WinUser = myReader["WIN_USER"] == DBNull.Value ? "" : (string) myReader["WIN_USER"];
            Photo = myReader["PHOTO"] == DBNull.Value ? "" : (string) myReader["PHOTO"];
            CatId = myReader["CAT_ID"] == DBNull.Value ? 0 : (decimal) myReader["CAT_ID"];
            var itemDao = new ItemDao();
            var item = itemDao.FindWinnerIfNeeded(this);
            Id = item.Id;
            Name = item.Name;
            Price = item.Price;
            StartTime = item.StartTime;
            EndTime = item.EndTime;
            SweepsOnItem = item.SweepsOnItem;
            Description = item.Description;
            OriginalPrice = item.OriginalPrice;
            WinUser = item.WinUser;
            Photo = item.Photo;
            CatId = item.CatId;
        }

        //Methods
        public void Fill()
        {
            _fill = false;
            _categoryObj = CategoryObj;
            _winUserObj = WinUserObj;
            _bidOnUsersList = BidOnUsersList;
            _loseUsersList = LoseUsersList;
            _bidOnsList = BidOnsList;
            _fill = true;
        }
    }
}